package io.github.lee.app.unsplash.ui.search.result

import android.os.Handler
import android.os.Looper
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint
import io.github.lee.app.unsplash.R
import io.github.lee.app.unsplash.databinding.FragmentSearchResultBinding
import io.github.lee.app.unsplash.ui.search.result.collection.ResultCollectionFragment
import io.github.lee.app.unsplash.ui.search.result.photo.ResultPhotoFragment
import io.github.lee.app.unsplash.ui.search.result.user.ResultUserFragment
import io.github.lee.core.ui.BaseFragment
import io.github.lee.core.vm.BaseViewModel

@AndroidEntryPoint
class SearchResultFragment : BaseFragment<FragmentSearchResultBinding, BaseViewModel>() {
    private var mediator: TabLayoutMediator? = null

    private val photoFragment by lazy { ResultPhotoFragment.newInstance() }
    private val collectionFragment by lazy { ResultCollectionFragment.newInstance() }
    private val userFragment by lazy { ResultUserFragment.newInstance() }

    companion object {
        fun newInstance(): SearchResultFragment = SearchResultFragment()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mediator?.detach()
    }

    //===Desc:======================================================================================
    override fun onCreateSuccess(): FragmentSearchResultBinding =
        FragmentSearchResultBinding.inflate(layoutInflater)

    //===Desc:======================================================================================


    override fun onSetViewData() {
        super.onSetViewData()
        vb?.vpSearchContent?.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int = 3
            override fun createFragment(position: Int): Fragment =
                when (position) {
                    0 -> photoFragment
                    1 -> collectionFragment
                    2 -> userFragment
                    else -> throw IllegalStateException("Unknown tab page")
                }
        }

        vb?.apply {
            val strategy =
                TabLayoutMediator.TabConfigurationStrategy { tab, position ->
                    val titles = resources.getStringArray(R.array.search_result_tab_titles)
                    tab.text = titles[position]
                }
            mediator = TabLayoutMediator(tabSearchTabs, vpSearchContent, strategy)
            mediator?.attach()
        }
    }

    //===Desc:======================================================================================

    fun search(keyword: String?) {
        if (keyword.isNullOrEmpty()) {
            return
        }
        Handler(Looper.getMainLooper()).postDelayed({
            photoFragment.search(keyword)
            collectionFragment.search(keyword)
            userFragment.search(keyword)
        }, 100L)

    }


}